<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      const orders = [
        {
          id: 1,
          name: 'John Smith',
          address: '123 Main St',
          status: 'created',
          log: [
            {
              status: 'created',
              time: '2023-02-16 12:30:00',
            },
          ],
        },
        {
          id: 2,
          name: 'Jane Doe',
          address: '456 Second St',
          status: 'packed',
          log: [
            {
              status: 'created',
              time: '2023-02-16 10:00:00',
            },
            {
              status: 'packed',
              time: '2023-02-16 12:30:00',
            },
          ],
        },
        {
          id: 4,
          name: 'Bob Johnson',
          address: '789 Third St',
          status: 'shipped',
          log: [
            {
              status: 'created',
              time: '2023-02-15 10:00:00',
            },
            {
              status: 'packed',
              time: '2023-02-15 12:00:00',
            },
            {
              status: 'shipped',
              time: '2023-02-16 9:00:00',
            },
          ],
        },
        {
          id: 3,
          name: 'Jane Johnson',
          address: '156 Third St',
          status: 'shipped',
          log: [
            {
              status: 'created',
              time: '2023-02-14 10:00:00',
            },
            {
              status: 'packed',
              time: '2023-02-15 12:00:00',
            },
            {
              status: 'shipped',
              time: '2023-02-16 9:00:00',
            },
          ],
        },
      ]
        let nextArr = []
        Object.assign(nextArr , orders)
      function getOrdersByStatus(status) {
        
        let str =nextArr.filter((item) => {
         return  item.status === status
        })
       
        let ar = str.sort((a ,b) =>{
          return a.id - b.id
        })
      
       ar.map(({log} ,index) =>{
        let newStr =  log.filter(item =>{
         return item.status === status
       })
      ar[index].log   = newStr
      })
      return ar
    }

    // function getOrderStatusHistory(id){
    //   let log 
    //   orders.forEach((item) =>{
    //   if(item.id === id){
    //    log = item.log
    //   }
    //   })
    //   let sum =[]
    //      log.forEach(item =>{
    //       sum.push(`[${item.time}]${item.status}`)
    //      })
         
    //      console.log(sum.join('\n'));
    // }
    //  getOrderStatusHistory(4)
      const newArr = getOrdersByStatus('shipped')
       console.log(newArr);
    </script>
  </body>
</html>
